package com.attendance.sz.local;

import android.database.Cursor;
import android.util.Log;

import com.attendance.sz.exception.DBException;


public class LocalCardRecordDao extends LocalBaseDao{
	
	public String table_name;
	
	public LocalCardRecordDao(){
		
	}
	
	private void initCardRecord() throws DBException{
		
		table_name = DBConstants.TABLE_NAMES[DBConstants.TABLE_CARD_RECORD];
		init();
	}
	
	/**
	 * After the landing successful, and the user's information deposit account in the table
	 * @param hashMap
	 * @return
	 * @throws DBException
	 */
	public Cursor GetCardRecordCurson() throws DBException{
		
		initCardRecord();
		/**
		 * limit 0,5 含义是指索引0开始，取5条 limit 5,6指索引5开始,取6条记录
		 */
		String strSql="Select * from "+table_name+" limit 0,5";
		
	    return dbHandler.query(strSql);

	}

	public Cursor GetCardRecordCursonExAdd( ) throws DBException{

		initCardRecord();
		/**
		 * limit 0,5 含义是指索引0开始，取5条 limit 5,6指索引5开始,取6条记录
		 * select * from card_record where uploadFlag = 0 limit 0,5
		 * select * from card_record where uploadFlag = 0 limit 5,5
		 * select * from card_record where uploadFlag = 0 limit 10,5
		 * select * from card_record where uploadFlag = 0 limit 15,5
		 */

		//String strSql="Select * from "+table_name+" limit 0,5";
		String strSql="Select * from " + table_name + " where uploadFlag == '0' order by id asc";


		return dbHandler.query(strSql);

	}
	public Cursor GetCardRecordCursonEx(int iStartIndex, int iCount) throws DBException{
		
		initCardRecord();
		/**
		 * limit 0,5 含义是指索引0开始，取5条 limit 5,6指索引5开始,取6条记录
		 * select * from card_record where uploadFlag = 0 limit 0,5
		 * select * from card_record where uploadFlag = 0 limit 5,5
		 * select * from card_record where uploadFlag = 0 limit 10,5
		 * select * from card_record where uploadFlag = 0 limit 15,5
		 */
		
		//String strSql="Select * from "+table_name+" limit 0,5";
		String strSql="Select * from " + table_name + " where uploadFlag == '0' order by id asc  limit ";
		strSql += iStartIndex;
		strSql += ",";
		strSql += iCount;
		strSql += " ";
		
	    return dbHandler.query(strSql);

	}

	public Cursor GetCardRecordCursonAuto(int iStartIndex, int iCount) throws DBException{

		initCardRecord();

		//String strSql="Select * from "+table_name+" limit 0,5";
		String strSql="Select * from "+table_name+ "  order by id asc  limit ";
		strSql += iStartIndex;
		strSql += ",";
		strSql += iCount;
		strSql += " ";

		return dbHandler.query(strSql);

	}
	
	public void AddCardRecord(String[] params) throws DBException{
		
		
		initCardRecord();
		/**
		 * add by martrin 20150801 增加三列:device_no, uploadFlag, upload_fail_count
		 */
		String insertSql = "insert into " + table_name+ "(card_no,line_no" +
				",name,card_type,department,create_time,device_no,uploadFlag,upload_fail_count) values(" +
				"'"+params[0]+"'," +
				"'"+params[1]+"'," +
				"'"+params[2]+"'," +
				"'"+params[3]+"'," +
				"'"+params[4]+"'," +
				"'"+params[5]+"'," +
				"'"+params[6]+"'," +
				"'"+params[7]+"'," +
				"'"+params[8]+"')";
		synchronized(dbHandler){
		dbHandler.execute(insertSql);
		}
		
	} 
	
	
	public void DeleteCardRecord(String[] params) throws DBException{
		
		
        initCardRecord();
		
		String insertSql = "delete from " + table_name;
		if(params!=null&&params.length>0)
		{
			insertSql=insertSql+" where id in (";
			for(int i=0;i<params.length;i++)
			{
				insertSql=insertSql+"'"+params[i]+"',";
			}
			
			insertSql=insertSql.substring(0,insertSql.length()-1);
			
			insertSql=insertSql+")";
			
			synchronized(dbHandler){
			dbHandler.execute(insertSql);
			}
		}
		
		
		
	}
	
	/**
	 * 更新卡的记录
	 * @param params
	 * @throws DBException
	 */
	public void UpdateCardRecord(String params, String sWhere) throws DBException{

        initCardRecord();
		//exam:update card_record set card_no = '2669CCDE', card_type = '02'  where line_no = '01000000'
		String sSql = "update " + table_name + " set ";
		sSql += params;
		sSql = " where line_no = " + sWhere + " ";
		
		synchronized(dbHandler){	
		dbHandler.execute(sSql);
		}
		
		
		
		
	}
	
	public boolean UpdateCardRecordFlag(String id){
		boolean bFlag = false;
		String sSql = "update " + table_name + " set uploadFlag = '1' ";
		sSql += " where id = " + id + " ";
		try{
			initCardRecord();
			synchronized(dbHandler){
			dbHandler.execute(sSql);
			}
			bFlag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			bFlag = false;
			e.printStackTrace();
			
		}finally{
			
			
		}
		
		return bFlag;
	}

	public boolean UpdateUploadFailCount(String id){
		boolean bFlag = false;
		String sSql = "update " + table_name + " set upload_fail_count = upload_fail_count + '1' ";
		sSql += " where id = " + id + " ";
		try{
			initCardRecord();
			synchronized(dbHandler){
				dbHandler.execute(sSql);
			}
			bFlag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			bFlag = false;
			e.printStackTrace();

		}finally{


		}

		return bFlag;
	}

	/**
	 * 取数据库中所有记录即总记录
	 * @return
	 * @throws DBException
	 */
	public int GetCardRecordTotal() throws DBException{
		
		initCardRecord();
		
		String strSql="Select * from "+table_name+" ";
		
		Cursor cur= dbHandler.query(strSql);
		int num = cur.getCount();
		cur.close();
	    return num;

	}
	
	/**
	 * 取数据库要上传记录
	 * @return
	 * @throws DBException
	 */
	public int GetCardRecordUpload() throws DBException{
		
		initCardRecord();
		
		String strSql="Select * from "+table_name+" where uploadFlag = '0' ";
		
		Cursor cur= dbHandler.query(strSql);
		int num = cur.getCount();
		cur.close();
		Log.i("TAG","数据上传");
	    return num;

	}

	/**
	 * 取数据库要上传记录
	 * @return
	 * @throws DBException
	 */
	public int deleteAllRecord() throws DBException{

		initCardRecord();

		String strSql="delete  from "+table_name;

		Cursor cur= dbHandler.query(strSql);
		int num = cur.getCount();
		cur.close();
		return num;

	}

}